package com.yonyou.cloud.member.adapters.openapi.controller;

import com.yonyou.cloud.member.adapters.dto.ApiResponseDto;
import com.yonyou.cloud.member.domainmodel.entities.member.Member;
import com.yonyou.cloud.member.domainmodel.vo.MemberId;
import com.yonyou.cloud.member.domainmodel.services.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "会员档案服务接口")
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    MemberService memberService;

    @GetMapping("/{phone}")
    @ApiOperation(value = "根据手机号码查找会员", httpMethod = "GET")
    @ApiResponse(code = 200, message = "会员信息", response = Member.class)
    public ApiResponseDto<Member> find(@ApiParam(name = "phone", value = "手机号码", required = true, defaultValue = "18910721990") @PathVariable("phone") String phone) {
        Member member = memberService.find(phone);
        return new ApiResponseDto(true).setData(member);
    }

    @GetMapping("/count")
    @ApiOperation(value = "获取会员总数", httpMethod = "GET")
    @ApiResponse(code = 200, message = "会员数量", response = Integer.class)
    public ApiResponseDto<Integer> count() {
        Integer count = memberService.count();
        return new ApiResponseDto(true).setData(count);
    }

    @GetMapping("/query")
    @ApiOperation(value = "根据会员级别查找会员", httpMethod = "GET")
    @ApiResponse(code = 200, message = "会员列表", response = Member.class, responseContainer = "List")
    public ApiResponseDto<List<Member>> list(@ApiParam(name = "level", value = "会员级别", required = true, defaultValue = "白金") @RequestParam("level") String level) {
        List<Member> members = memberService.list(level);
        return new ApiResponseDto(true).setData(members);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增会员", httpMethod = "POST")
    @ApiResponse(code = 200, message = "是否成功", response = Boolean.class)
    public ApiResponseDto<Boolean> add(@ApiParam(name = "member", value = "会员信息", required = true) @RequestBody Member member) {
        boolean result = memberService.add(member);
        return new ApiResponseDto(result);
    }

    @GetMapping("/delete/{id}")
    @ApiIgnore
    @ApiOperation(value = "根据ID删除用户", httpMethod = "GET")
    @ApiResponse(code = 200, message = "是否成功", response = Boolean.class)
    public ApiResponseDto<Boolean> delete(@ApiParam(name = "id", value = "会员ID", required = true) @PathVariable("id") Long id) {
        boolean result = memberService.delete(new MemberId(id));
        return new ApiResponseDto(result);
    }

//    @RequestMapping("/")
//    public String index() {
//        return "forward:index.html";
//    }

}
